home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-20 | 5.0 KB | 166 lines | [TEXT/ALFA] |
-
-
- Assembly and QForth
- -------------------
-
-
- All of the traps provided as extensions to QForth can be used from
- assembly language as well. However, several of them insist that their
- parameters be on the QForth stack at call time and several return
- their results to the QForth stack. This little file details how to
- use the QForth stack from within an assembly language program.
-
-
- ================================================================================
-
-
- 1. QForth stack structure and important locations
- 2. Traps which use the QForth stack
- 3. Programming examples
-
-
-
-
- The QForth Stack
- ----------------
-
- QForth maintains a one page stack of 16-bit numbers from $AA00-$AAFF,
- starting at $AAFF and growing downward. The current stack pointer,
- which points to the location for the next item pushed on the stack, is
- in $EE. The current stack depth is stored in location $F4. Of interest
- to assembly language programmers are the two QForth subroutines PUSHDATA
- and POPDATA which can be used to push and pop values from the QForth data
- stack:
-
-
- Name Location Registers used
- -------------------------------------------------
- PUSHDATA $20B2 Y (low), X (high) -> stack, A altered
- POPDATA $20CA stack -> Y (low), X (high), A altered
-
- Other useful QForth words are:
-
- DUP $3519 ( n -- n n )
- DROP $3531 ( n -- ) ( really the same as POPDATA )
- SWAP $353B ( n m -- m n )
- OVER $355E ( n m -- n m n )
- ROT $3583 ( i j k -- j k i )
-
- Note: these subroutines alter A, X, and Y.
-
-
-
- Traps that use the QForth stack
- -------------------------------
-
- The following traps use the QForth stack for passing data:
-
-
- Name Trap location Use
- ------------------------------------------------------------------
- Color $FFC0 ( n -- ) Expects new drawing color on stack
- LineTo $FFB0 ( x y -- ) Line from current pen to x,y on stack
- MoveTo $FFA0 ( x y -- ) Set pen to x,y on stack
- Plot $FFD0 ( x y -- ) Plot a point at x,y
-
- Mouse $FFE0 ( -- x y b ) Puts mouse position x,y on stack with
- button status= 0 (up), FFFF (-1) (down)
-
- CH $3A9A ( n -- ) New horizontal position on stack
- CV $3A8C ( n -- ) New vertical position on stack
-
- Random2 $FF92 ( n -- n' ) N on stack, returns n' = 0..n-1 on stack
-
-
- Where ( before -- after ) is the usual Forth convention for indicating
- stack values before and after calling a word.
-
-
-
- Programming Examples
- --------------------
-
- The following examples are also in the DEMO folder as DEMO1.S,
- DEMO2.S, and DEMO3.S. Assemble them with the command line:
-
- asm6502 -o :demo:demo1.s
-
- , etc.
-
-
-
- 1. Plotting a line
-
-
- PUSH = $20B2 ; QForth stack equates
- POP = $20CA
- MOVETO = $FFA0 ; MoveTo
- LINETO = $FFB0 ; LineTo
-
- *= $0300 ; start here
-
- ldy #$0a ; plot from 10,10 to 200,200
- ldx #$00
- jsr PUSH
- ldy #$0a
- ldx #$00
- jsr PUSH ; push two 10s on the stack ( x y -- )
- jsr MOVETO ; set drawing pen
- ldy #$c8
- ldx #$00
- jsr PUSH
- ldy #$c8
- ldx #$00
- jsr PUSH ; push two 200s on the stack ( x y -- )
- jsr LINETO ; draw the line
- rts
-
-
-
- 2. Waiting for the mouse button to be pressed
-
-
- DROP = $3531 ; could use POPDATA here as well
- MOUSE = $FFE0
- BUTTON = $3FF ; hold low part of button status
- *= $0300
-
- LOOP jsr MOUSE ; get the mouse
- jsr DROP ; DROP = POPDATA, so data is in Y (lo) and X (hi)
- sty BUTTON ; save low byte of button status
- jsr DROP ; remove mouse location data
- jsr DROP
- lda BUTTON
- cmp #$FF ; is it FF, low part of FFFF = -1, button down?
- bne LOOP ; no. keep waiting
- rts ; yes. all done
-
-
-
- 3. Print 20 random numbers from 0..100
-
-
- PUSH = $20B2 ; QForth stack equates
- POP = $20CA
- RND2 = $FF92 ; random number off stack
- HOUT = $FDDA ; output A as a hex number
- COUT = $FDED ; output A as a character
- COUNT = $3FF ; count in here
-
- *= $0300
-
- lda #$14 ; setup count
- sta COUNT
- LOOP ldy #$64 ; 100 in Y and X
- ldx #$00
- jsr PUSH ; ( 100 -- )
- jsr RND2 ; ( -- 0..100 )
- jsr POP ; in Y and X
- tya ; n' -> A
- jsr HOUT ; print it
- lda #$0D ; carriage return
- jsr COUT
- dec COUNT ; count <- count - 1
- bne LOOP ; not done
- rts ; done
-